#include "gamemaster.h"
#include "card.h"
#include "Hand.h"
#include "Player.h"
#include "AIPlayer.h"
#include "NetworkPlayer.h"
#include <cstring>
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <QStringList>
#include "game.h"
#include <QtCore>

// Network header files
#include "Client.h"
#include "Server.h"
#include "ClientHandle.h"
using namespace std;


GameMaster::GameMaster(QObject *parent) : QObject(parent)
{

    idShuffled = new int[52];
    initializeDeck();



    for(int i=0; i<4;i++)      //beginning of the game the score is 0
     {

        scores[i]= 0;
        TrickWins[i]=0;
     }


}

GameMaster::GameMaster(game *f)
{
  gameGui=f;
  idShuffled = new int[52];
  initializeDeck();
    count=0;

  for(int i=0; i<4;i++)      //beginning of the game the score is 0
   {

      scores[i]= 0;
      TrickWins[i]=0;
   }
  numTricks=0;
  numDeals=0;
  numGames=0;

  Hand h;
      players[0] = new NetworkPlayer(h,0,gameGui,server);
      players[1] = new Player(h,1,gameGui,server);
      players[2] = new Player(h,2,gameGui,server);
      players[3] = new Player(h,3,gameGui,server);


}

GameMaster::~GameMaster()
{
    //delete [] scores;
   // delete [] contract;
    //delete  _cards;
    delete idShuffled;
    delete players;

}

QString GameMaster::getServerIP()
{
    return server->getIP();
}

void GameMaster::initializeDeck()
{
    char o= '\0';   // NULL: dont assing onwner yet
    int j=0;   //iterarator
    string num;
    string sere;
    char *s = new char[2];
    int temp;
    for(int i=0;i<9;i++)
    {
        //itoa(i+2,s,10);
        QString str = QString::number(i+2);
        sere=str.toStdString();

        //temp = i+2;
        num ="0" +sere;

        deck.push_back(Card('S',i+1,o,num));
    }
    deck.push_back(Card('S',10,o,"0J"));
    deck.push_back(Card('S',11,o,"0Q"));
    deck.push_back(Card('S',12,o,"0K"));
    deck.push_back(Card('S',13,o,"0A"));
    for(int i=13;i<21;i++)
    {
        //itoa(j+2,s,10);
        QString str = QString::number(j+2);
        sere=str.toStdString();
        num ="0" +sere;
        deck.push_back(Card('H',i+1,o,num));
        j++;
    }
    deck.push_back(Card('H',22,o,"10"));
    deck.push_back(Card('H',23,o,"0J"));
    deck.push_back(Card('H',24,o,"0Q"));
    deck.push_back(Card('H',25,o,"0K"));
    deck.push_back(Card('H',26,o,"0A"));
    j=0;
    for(int i=26;i<34;i++)
    {
        //itoa(j+2,s,10);
        QString str = QString::number(j+2);
        sere=str.toStdString();
        num ="0" +sere;
        deck.push_back(Card('D',i+1,o,num));
        j++;
    }
    deck.push_back(Card('D',35,o,"10"));
    deck.push_back(Card('D',36,o,"0J"));
    deck.push_back(Card('D',37,o,"0Q"));
    deck.push_back(Card('D',38,o,"0K"));
    deck.push_back(Card('D',39,o,"0A"));
    j=0;
    for(int i=39;i<47;i++)
    {
        //itoa(j+2,s,10);
        QString str = QString::number(j+2);
        sere=str.toStdString();
        num ="0" +sere;
        deck.push_back(Card('C',i+1,o,num));
        j++;
    }
    deck.push_back(Card('C',48,o,"10"));
    deck.push_back(Card('C',49,o,"0J"));
    deck.push_back(Card('C',50,o,"0Q"));
    deck.push_back(Card('C',51,o,"0K"));
    deck.push_back(Card('C',52,o,"0A"));
}
void GameMaster::shuffle() //shuffles the IDs
{
    int ids[52];        // to store card IDs
    int random;
    unsigned int seed = time(0);
    srand(1);                // seed the rand num generator
    //bool status = true;
    for(int i = 0; i < 52; i++)
    {

        if(i == 0)
        {
            random = 1 + rand()% 52;
            ids[i]= random;
        }
        else
        {
            random = 1 + rand()% 52;
            while (helperShuffle(i, random,ids)== true) // do when there is a duplicate
            {
                random = 1 + rand()% 52;
                helperShuffle(i,random,ids);
            }
            ids[i]= random;
        }

    }

    for(int i=0;i<52;i++) // print out shuffle IDs
    {
        idShuffled[i]=ids[i];

      //  cout<<idShuffled[i]<<",";   // for testing purposes
    }
    qDebug()<<"end shuffle";

}
bool GameMaster::helperShuffle(int size,int random,int ids[])
{
    bool status = false;
    for(int j = 0; j < size; j++)
    {
       if( ids[j] == random)
           status = true;         // true => there is a duplicate
    }
    return status;
}


void GameMaster::deal()     // assign card to owner
{

    numDeals++;
    Hand h1;
    Hand h2;
    Hand h3;
    Hand h4;
    int i;     //position in idShuffled

    for(i=0;i<52;i++)
    {
        for(list<Card>::iterator pos = deck.begin(); pos != deck.end();pos++)
        {
            if(pos->id == idShuffled[i])
            {

               if(i == 0 || i<13)
               {
                 pos->owner=1;
                 h1.addCard(*pos);


               }
               else if(i == 13 || i<26)
               {
                    pos->owner=2;
                 h2.addCard(*pos);

               }
               else if(i == 26 || i<39)
               {
                    pos->owner=3;
                 h3.addCard(*pos);

               }
               else
               {
                    pos->owner=4;
                 h4.addCard(*pos);


               }

            }
        }
    }

    //inform Players about the beginning of dealing
    /*
    for(int i=0; i<4;i++)
     {

        //players[i].dealingStart();    //inform players about dealing
     }

    */

   // delete []players;

    players[0]->setLocalHand(h1);
    players[1]->setLocalHand(h2);
    players[2]->setLocalHand(h3);
    players[3]->setLocalHand(h4);

    srand(1);
    int d = 0 + rand()% 3;
   // cout<<" player that deals "<<d<<"\n";
    players[d]->dealer = true;

    qDebug()<<"done dealing";
    startSendHand();



}
void GameMaster::startSendHand()
{
    qDebug()<<"startSendHand";
    curr_player=0;
     players[curr_player]->sendHand();
}

void GameMaster::ack_sendHand()
{
    curr_player++;
    if(curr_player>3)
        startBidding();
    else
     players[curr_player]->sendHand();
}

void GameMaster::startDealing()
{
    qDebug()<<"start dealing";
    curr_player=0;
     //qDebug()<<"here";
     players[curr_player]->startDealing();
    qDebug()<<"start dealing first player";

}
void GameMaster::ack_startDealing()
{
    qDebug()<<"ack_startDealing";
    curr_player++;
    if(curr_player>3)
        deal();
    else
     players[curr_player]->startDealing();
}

void GameMaster::ack_cards()
{
   curr_player++;
   if(curr_player>3)
       startBidding();
}

void GameMaster::startBidding()
{
    //inform Players about bidding
    qDebug()<<"start bidding";
    curr_player=0;

     players[curr_player]->startBidding();
}
void GameMaster::ack_startBidding()
{
     curr_player++;
     if(curr_player>3)
         bidding();
    else
     players[curr_player]->startBidding();
}
void GameMaster::bidding()
{
     curr_player=0;
     players[curr_player]->getBid();
}
void GameMaster::sendBid(QStringList bid)
{

    int pos = 0;        //keeping track of players, clockwise
    //count= 0;    // index in bids
   // QStringList bid;     // stores current bid string
    string bids[4];

    qDebug()<<bid.at(0)<<bid.at(1)<<bid.at(2);

    if(bid.at(0).compare("P")==0){
       ++passCount;

    }
    else
       passCount=0;

    bids[count] = bid.at(2).toUtf8().constData();
    bids[count].append(bid.at(1).toUtf8().constData());
    highBid(bids[count],pos);

    string a = bid.at(0).toUtf8().constData();
    a.append(bid.at(2).toUtf8().constData());
    a.append(bid.at(1).toUtf8().constData());

    string validate = bid.at(2).toUtf8().constData();
    validate.append(bid.at(1).toUtf8().constData());

     if(!validateBid(bids ,validate,a, count))
     {
            qDebug()<<"Invalid";
             players[curr_player]->bidInvalid();
             players[curr_player]->getBid();
     }
     else
        {
            count++;
            count=count%4;
            lastBid=bid;
            lastBidUsername= players[curr_player]->getUsername();
            if(bid.at(0).compare("P")!=0)
                highestBid=bid;
            startSendBid();
        }
}

void GameMaster::startSendBid()
{
    qDebug()<<"start send bid";
    ack_playerCounter=0;
    lastBid.append(QString::fromStdString(lastBidUsername));
    players[ack_playerCounter]->sendBid(lastBid,ack_playerCounter);
}

void GameMaster::ack_sendBid()
{
    qDebug()<<"ack sendBid";
     ack_playerCounter++;
     if(ack_playerCounter>3)
     {
         if(passCount==3)
         {

             int p = 0;    //player with maxBid
             int max = validateHelper2(players[0]->getHighestBid());
             for(int i =1; i<4;i++)
             {
               if(max < validateHelper2(players[i]->getHighestBid()))
               {
                   max = validateHelper2(players[i]->getHighestBid());
                   p = i;

               }
             }


             players[p]->declarer = true;
             p=p+2;
             p=p%4;
             players[p]->dummyFlag = true;
             dummyId= players[p]->getUsername();
             dummyHand=players[p]->localHand;
             firstPlayer=p-1;
             firstPlayer=firstPlayer%4;

             startSendContract();

         }
         else
             if(passCount==4)
             {
                startRedealing();
             }
             else
             {
                 qDebug()<<"get next bid";
                 curr_player++;
                 curr_player=curr_player%4;
                 players[curr_player]->getBid();
             }
    }
     else
     {
          lastBid.append(QString::fromStdString(lastBidUsername));
         players[ack_playerCounter]->sendBid(lastBid,ack_playerCounter);
     }
}

void GameMaster::startRedealing()
{
    ack_playerCounter=0;
     players[ack_playerCounter]->startRedealing();
}

void GameMaster::ack_startRedealing()
{
    ack_playerCounter++;
    if(ack_playerCounter>3)
    {
        shuffle();
        deal();
    }
    else
     players[ack_playerCounter]->startRedealing();
}

void GameMaster::startSendContract()
{
    ack_playerCounter=0;
    players[ack_playerCounter]->sendContract(highestBid);
}

void GameMaster::ack_sendContract()
{
    ack_playerCounter++;
    if(ack_playerCounter>3)
    {
        startDummy();
    }
    else
    players[ack_playerCounter]->sendContract(highestBid);
}

void GameMaster::startDummy()
{
    curr_player=0;
    players[curr_player]->startDummy();
}

void GameMaster::ack_startDummy()
{
    curr_player++;
    if(curr_player>3)
    {
        startSendDummyId();
    }
    else
        players[curr_player]->startDummy();
}
void GameMaster::startSendDummyId()
{
    curr_player=0;
    players[curr_player]->sendDummyId(dummyId);
}
void GameMaster::ack_startSendDummyId()
{
    curr_player++;
    if(curr_player>3)
    {
        startSendDummyHand();
    }
    else
        players[curr_player]->sendDummyId(dummyId);
}
void GameMaster::startSendDummyHand()
{
    curr_player=0;
    players[curr_player]->sendDummyCards(dummyHand);
}

void GameMaster::ack_startSendDummyHand()
{
    curr_player++;
    if(curr_player>3)
    {

    }
    else
     players[curr_player]->sendDummyCards(dummyHand);
}
void  GameMaster::startGame()
{
    curr_player=0;
    players[curr_player]->startPlay();
}

void  GameMaster::ack_startGame()
{
    curr_player++;
    if(curr_player>3)
    {
        startSendGameUpdate();
    }
    else
        players[curr_player]->startPlay();
}
void GameMaster::startSendGameUpdate()
{
    QStringList status;
    status.append(QString::number(numTricks));
    status.append(QString::number(numDeals));
    status.append(QString::number(numGames));

    curr_player=0;
    players[curr_player]->sendGameStatus(status);
}

void GameMaster::ack_startSendGameUpdate()
{
    curr_player++;
    if(curr_player>3)
    {
        startPlaying();
    }
    else
    {
        QStringList status;
        status.append(QString::number(numTricks));
        status.append(QString::number(numDeals));
        status.append(QString::number(numGames));
        players[curr_player]->sendGameStatus(status);
    }
}

void GameMaster::startPlaying()
{
    trick = new Hand();
    numTricks = 1;
    leadSuit=' ';
    curr_player=firstPlayer;
    players[curr_player]->getMove(false);
}
void GameMaster::sendMove(Card card)
{


    // numTricks = 1; // total number of trick = 13
    int pos;
    char leadSuit;


    if(!validateMove(leadSuit,card,pos))//Move not valid
    {
       players[curr_player]->getMove(false);
    }
    else
    {

        trick->addCard(card);
        startSendMove();

       // curr_player++;
       // curr_player=curr_player%4;
        //players[curr_player]->getMove(false);

    }

}
void GameMaster::startSendMove()
{
    ack_playerCounter=0;
    players[ack_playerCounter]->sendMove(*trick->getCard(trick->getHandSize()-1),ack_playerCounter);
}

void GameMaster::ack_sendMove()
{
    ack_playerCounter++;
    if(ack_playerCounter>3)
    {
        if(trick->getHandSize()==4)
        {
            UpdateTrickWinner();
            startUpdateTrickWinner();

        }
        else
        {
            curr_player++;
            curr_player=curr_player%4;

            if(players[curr_player]->dummyFlag)
            {
                players[curr_player]->partner->getMove(true);
            }
            else
                players[curr_player]->getMove(false);
        }
    }
    else
    {
        players[ack_playerCounter]->sendMove(*trick->getCard(trick->getHandSize()-1),ack_playerCounter);
    }

}

void GameMaster::startUpdateTrickWinner()
{
    ack_playerCounter=0;
    players[ack_playerCounter]->sendTrickWinner(players[lastTrickWinner]->getUsername());
}

void GameMaster::ack_startUpdateTrickWinner()
{
    ack_playerCounter++;
    if(ack_playerCounter>3)
    {
        if( numTricks>13)
        {
            determineScore();
            startUpdateScore();
        }
        else
            startSendGameUpdate();
    }
    else
        players[ack_playerCounter]->sendTrickWinner(players[lastTrickWinner]->getUsername());
}
void GameMaster::startUpdateScore()
{
    ack_playerCounter=0;
    players[ack_playerCounter]->sendScoreUpdate("");
}

void GameMaster::ack_startUpdateScore()
{
    ack_playerCounter++;
    if(ack_playerCounter>3)
    {
        determinGameWinner();
        startSendWinner();
    }
    else
     players[ack_playerCounter]->sendScoreUpdate("");
}
void GameMaster::startSendWinner()
{
    ack_playerCounter=0;
    players[ack_playerCounter]->sendScoreUpdate("");
}

void GameMaster::ack_startSendWinner()
{
    ack_playerCounter++;
    if(ack_playerCounter>3)
    {
        //we are done!!!
    }
    else
        players[ack_playerCounter]->sendScoreUpdate("");
}

void GameMaster::ack_getMove()
{

}

/*
    for(int i=0; i<4;i++)
     {

        players[i]->startBidding();    //inform players about dealing
     }
    //qDebug() << "first loop";


    int pos = 0;        //keeping track of players, clockwise
    int count= 1;    // index in bids
    QStringList bid;     // stores current bid string
    string bids[4];

    string allBids[15];  //  to store all bids
    int allBidsCount = 1 ;// index for allBids

    for(int i=0;i<4;i++)
    {
        if(players[i]->dealer == 1)   //find player that goes first
        {
            pos = i+1;

        }
    }

    //qDebug() << "2nd loop";
    pos = pos % 4;
    count = count%4;
    bid=players[pos]->getBid();

   //qDebug()<<bid;



    bids[count] = bid.at(2).toUtf8().constData();
    bids[count].append(bid.at(1).toUtf8().constData()); //first player's bid

    highBid(bids[count],pos); // stores the highest bid of a player


    allBids[allBidsCount]= bids[count];
    allBidsCount++;

   // cout<<bids[count]<<" bid"<<endl;


    //update GUI about first bid
    //send bid to other players

    //inform Players about a valid bid

    for(int i=0; i<4;i++)
     {

        players[i]->sendBid( bid,i);
     }

//qDebug() << "3rd loop";
    bool done = false;
    int passCount= 0;

    while(!done)
    {

          while(passCount<3)
          {

              pos++;          // prepare for next player
              pos = pos % 4;

              bid = players[pos]->getBid();
              //qDebug()<<bid;

              string a = bid.at(0).toUtf8().constData();
              a.append(bid.at(2).toUtf8().constData());
              a.append(bid.at(1).toUtf8().constData());
              string validate = bid.at(2).toUtf8().constData();
              validate.append(bid.at(1).toUtf8().constData());


             // cout<<"a: "<<a<<endl;


              while(!validateBid(bids ,validate,a, count))
              {

                cout<<"Invalid move\n";
                //update GUI about invalid bid

                players[pos]->bidInvalid();
                bid = players[pos]->getBid();
                //qDebug()<<bid;
                //cout<<"a: "<<a<<endl;
                return;


              }



               count++;
               count = count%4;
               bids[count] = bid.at(2).toUtf8().constData();
               bids[count].append(bid.at(1).toUtf8().constData());
               // bids[count].append(bid.at(0).toUtf8().constData());
              // cout<<bids[count]<<" bid2"<<endl;
               highBid(bids[count],pos); // stores the highest bid of a player


              // cout<< "here\n"<<endl;

               //update GUI about valid bid
               //send bid to other players

               //inform Players about a valid bid

               for(int i=0; i<4;i++)
                {

                   players[i]->sendBid(bid,i);
                }


               allBids[count] = bid.at(2).toUtf8().constData();
              allBids[count].append(bid.at(1).toUtf8().constData());


              allBidsCount++;


              if(bid.at(0).compare("P")==0){
                 ++passCount;

              }
              else
                 passCount=0;

              if(passCount >= 3)
              {
                if(bids[0]=="P" && bids[1]=="P" && bids[2]=="P" && bids[3]=="P")
                {
                    redeal();
                    done = true;

                }
                else
                {

                    done = true;
                    int p = 0;    //player with maxBid
                    int max = validateHelper2(players[0]->getHighestBid());
                    for(int i =1; i<4;i++)
                    {
                      if(max < validateHelper2(players[i]->getHighestBid()))
                      {
                          max = validateHelper2(players[i]->getHighestBid());
                          p = i;

                      }
                    }


                    players[p]->declarer = true;
                    p=p+2;
                    p=p%4;
                    players[p]->dummyFlag = true;



                }

             }
             else
              {
                done = false;


              }

          }


    }
    int o;
    for(int i=0; i<4; i++){
        if(players[i]->declarer==1)
          o=i;
    }

    string hBid = players[o]->getHighestBid();
    string temp;
   // cout<<"o: "<<hBid<<endl;
    temp = players[o]->specials + ",";


        temp=temp+hBid.at(0)+","+hBid.at(1);

        //cout<<"here"<<endl;

    //cout<<"temp: "<<temp <<endl;
   // cout<<"highBId"<<hBid<<endl;
    for (int j= 0; j<4 ; j++)
    {
        players[j]->sendContract(temp);
    }




    for(int i = 0; i<4;i++)   // for testing purposes
    {
        //cout<<"Bid array = "<<bids[count]<<",";
    }

   // cout<<"\n";

   // cout<<"ALL BIDs = ";
    for(int i = 0; i<15;i++)   // for testing purposes
    {
        //cout<<allBids[count]<<",";
    }

   // cout<<"\n";

    for(int i=0; i<4;i++)   // for testing purposes
     {
        players[i]->print();
     }




    //inform Players about bidding done
    /*
    for(int i=0; i<4;i++)
     {

        //players[i].biddingDone();    //inform players about dealing
     }




}*/

void GameMaster::highBid(string b,int pos)
{
    if(b != "P")
    {
        players[pos]->setHighestBid(b);
        players[pos]->contract = 6 + (players[pos]->getHighestBid().at(1)-48);
        players[pos]->specials = "B";

    }

    if (b== "D"|| b== "RD")
    {
        players[pos]->specials = b;

    }


}

bool GameMaster:: validateBid(string bids[],string b,string a,int count)
{
  // cout<<"a valid: "<<a<<endl;
   qDebug()<<"count = "<<count;
    if(count==0)
        return true;
    if(a[0]=='P'|| a[0]=='D' || a[0]=='R' )    //PASS,D:Double,RD:Redouble are always valid
    {

        return true;
    }

    else
    {

        if((validateHelper(b)>validateHelper(bids[count]))||bids[count]=="P"){

            return true;
        }
        else{

           // cout<<"bids: "<<b<<endl;
            return false;
        }
     }



}
int GameMaster::validateHelper(string b)
{
    qDebug()<<QString::fromStdString(b);
    int val;

        if(b.compare("C1")==0){val = 1;}
        else if(b.compare("D1")==0){val = 2;}
        else if(b.compare("H1")==0){val = 3;}
        else if(b.compare("S1")==0){val = 4;}
        else if(b.compare("NT1")==0){val = 5;}
        else if(b.compare("C2")==0){val = 6;}
        else if(b.compare("D2")==0){val = 7;}
        else if(b.compare("H2")==0){val = 8;}
        else if(b.compare("S2")==0){val = 9;}
        else if(b.compare("NT2")==0){val = 10;}
        else if(b.compare("C3")==0){val = 11; }
        else if(b.compare("D3")==0){val = 12; }
        else if(b.compare("H3")==0){val = 13; }
        else if(b.compare("S3")==0){val = 14; }
        else if(b.compare("NT3")==0){val = 15; }
        else if(b.compare("C4")==0){val = 16; }
        else if(b.compare("D4")==0){val = 17; }
        else if(b.compare("H4")==0){val = 18; }
        else if(b.compare("S4")==0){val = 19; }
        else if(b.compare("NT4")==0){val = 20; }
        else if(b.compare("C5")==0){val = 21; }
        else if(b.compare("D5")==0){val = 22; }
        else if(b.compare("H5")==0){val = 23; }
        else if(b.compare("S5")==0){val = 24; }
        else if(b.compare("NT5")==0){val = 25; }
        else if(b.compare("C6")==0){val = 26; }
        else if(b.compare("D6")==0){val = 27; }
        else if(b.compare("H6")==0){val = 28; }
        else if(b.compare("S6")==0){val = 29; }
        else if(b.compare("NT6")==0){val = 30; }
        else if(b.compare("C7")==0){val = 31; }
        else if(b.compare("D7")==0){val = 32; }
        else if(b.compare("H7")==0){val = 33; }
        else if(b.compare("S7")==0){val = 34; }
        else if(b.compare("NT7")==0){val = 35; }
        else{ val=0;}

        return val;
}
int GameMaster::validateHelper2(string b)
{
    int val;

        if(b.compare("C1")==0){val = 1;}
        else if(b.compare("D1")==0){val = 2;}
        else if(b.compare("H1")==0){val = 3;}
        else if(b.compare("S1")==0){val = 4;}
        else if(b.compare("NT1")==0){val = 5;}
        else if(b.compare("C2")==0){val = 6;}
        else if(b.compare("D2")==0){val = 7;}
        else if(b.compare("H2")==0){val = 8;}
        else if(b.compare("S2")==0){val = 9;}
        else if(b.compare("NT2")==0){val = 10;}
        else if(b.compare("C3")==0){val = 11; }
        else if(b.compare("D3")==0){val = 12; }
        else if(b.compare("H3")==0){val = 13; }
        else if(b.compare("S3")==0){val = 14; }
        else if(b.compare("NT3")==0){val = 15; }
        else if(b.compare("C4")==0){val = 16; }
        else if(b.compare("D4")==0){val = 17; }
        else if(b.compare("H4")==0){val = 18; }
        else if(b.compare("S4")==0){val = 19; }
        else if(b.compare("NT4")==0){val = 20; }
        else if(b.compare("C5")==0){val = 21; }
        else if(b.compare("D5")==0){val = 22; }
        else if(b.compare("H5")==0){val = 23; }
        else if(b.compare("S5")==0){val = 24; }
        else if(b.compare("NT5")==0){val = 25; }
        else if(b.compare("C6")==0){val = 26; }
        else if(b.compare("D6")==0){val = 27; }
        else if(b.compare("H6")==0){val = 28; }
        else if(b.compare("S6")==0){val = 29; }
        else if(b.compare("NT6")==0){val = 30; }
        else if(b.compare("C7")==0){val = 31; }
        else if(b.compare("D7")==0){val = 32; }
        else if(b.compare("H7")==0){val = 33; }
        else if(b.compare("S7")==0){val = 34; }
        else if(b.compare("NT7")==0){val = 35; }
        else{ val = -1;}

        return val;
}

void GameMaster::redeal() // enter when u encounter four passes
{

    for(int i=0; i<4;i++)
     {

        players[i]->startRedealing();    //inform players about dealing
     }
    //shuffle();
    //deal();
   // bidding();

    cout<<"\n Redealing \n";
}


void GameMaster::UpdateTrickWinner()
{
   // cout<<"trick = |";
    //trick->print();
    //cout<<"|\n";

    lastTrickWinner=trick->determineWinner();
     numTricks++;
    //cout<<"y: "<<y<<"|";

    ++TrickWins[lastTrickWinner];


}
bool GameMaster::validateMove(char leadSuit,Card h, int pos)  //returns true:1 if move is valid
{
    if(trick->getHandSize()<=0)
    {
        leadSuit=h.suit;
        return true;
    }
    if (leadSuit==h.suit||h.trump==1)
        return true;
    if(!players[pos]->hasCardsInSuit(leadSuit))
        return true;
    else
        return false;
}
void GameMaster::determineScore()
{
      int ns = 0;   // to store the total score of the team NS
      int ew = 0;   // to store the total score of the team EW

     ns = scores[0]+ scores[2];
     ew = scores[1]+ scores[3];

     //update scores[]
     scores [0] = ns;
     scores [2] = ns;
     scores [1] = ns;
     scores [3] = ns;

     //inform Players about scores
     /*
     for(int i=0; i<4;i++)
      {

         //players[i].gameStart();
      }

     */

}
string GameMaster::determineWinner()
{
  return "";
}

void GameMaster:: print()
{
    for(list<Card>::iterator pos = deck.begin(); pos != deck.end();pos++)
    {
          pos->print();
          cout<<",";

    }


   // cout<<"\n";
}
void GameMaster::determinGameWinner()
{
    if(TrickWins[0]+TrickWins[2]>TrickWins[1]+TrickWins[3])
    {
        gameWinners[numGames]=1;
    }
    else
        gameWinners[numGames]=2;

    numGames++;
}

